"use strict";

const Controller = require("egg").Controller;

class TagsController extends Controller {
  async index() {
    const { ctx, app } = this;

    const data = await app.mysql.select("tags");

    ctx.body = {
      code: 1,
      data,
    };
  }

  async add() {
    const { ctx, app } = this;

    const { label, value } = ctx.request.body;

    const data = await app.mysql.get("tags", { label });

    if (!data) {
      await app.mysql.insert("tags", {
        label,
        value,
      });

      ctx.body = {
        code: 1,
        msg: "操作成功",
      };
    } else {
      ctx.status = 401;

      ctx.body = {
        code: 0,
        msg: "该分类已存在",
      };
    }
  }

  async update() {
    const { ctx, app } = this;

    const { id, label, value } = ctx.request.body;

    await app.mysql.update("tags", { id, label, value });

    ctx.body = {
      code: 1,
      msg: "操作成功",
    };
  }

  async del() {
    const { ctx, app } = this;

    const { id } = ctx.request.body;

    await app.mysql.delete("tags", { id });

    ctx.body = {
      code: 1,
      msg: "操作成功",
    };
  }
}

module.exports = TagsController;
